using System;
using System.Collections.Generic;
using System.Text;
using System.Resources;
using System.Reflection;
using Microsoft.BizTalk.Message.Interop;

namespace eliasen.eu.BizTalk.PipelineComponents
{
    internal static class Util
    {
        internal static ResourceManager GetResourceManager()
        {
            return new System.Resources.ResourceManager("eliasen.eu.BizTalk.PipelineComponents.eliasen.eu.BizTalk.PipelineComponents", Assembly.GetExecutingAssembly());
        }

        internal static void WriteMessageContext(Microsoft.BizTalk.Message.Interop.IBaseMessageContext iBaseMessageContext)
        {
            uint counter = iBaseMessageContext.CountProperties;
            if (counter > int.MaxValue)
            {
                throw new Exception("The \"Write Properties\" pipeline component failed because the message that passed through the component has more than (2^31) - 1 (2147483647) properties.");
            }
            for (int i = 0; i < counter; i++)
            {
                StringBuilder sb = new StringBuilder();
                string name;
                string ns;
                iBaseMessageContext.ReadAt(i, out name, out ns);
                bool isPromoted = iBaseMessageContext.IsPromoted(name, ns);
                ContextPropertyType type = iBaseMessageContext.GetPropertyType(name, ns);
                sb.Append("Namespace: " + ns);
                sb.Append("\r\nName: " + name);
                sb.Append("\r\nisPromoted: " + isPromoted.ToString());
                sb.Append("\r\nType: " + type.ToString());
                System.Diagnostics.EventLog.WriteEntry("eliasen.eu pipeline component", sb.ToString());
            }
        }

        internal static void WriteEvent(string p)
        {
            if (p.Length > 32766)
                p = p.Substring(0, 32500) + "\r\n\r\nThe rest of the string was removed to make sure it fits in an eventlog entry.";
            System.Diagnostics.EventLog.WriteEntry("eliasen.eu pipeline component", p);
        }
    }
}
